package root.electronclass.electronclass.grade.record

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.FragmentClassListBinding
import root.electronclass.electronclass.databinding.FragmentClassListItemBinding
import root.electronclass.electronclass.databinding.FragmentClassRecordItemBinding
import root.electronclass.electronclass.ext.*
import root.electronclass.electronclass.grade.model.ClassRecord
import root.electronclass.electronclass.other.PictureActivity
import com.facebook.drawee.view.SimpleDraweeView
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList

/**
 * 班级风采
 * @author Lokiy
 * @date 2023/2/5 13:19
 */
class ClassRecordFragment : ViewBindingFragment<FragmentClassListBinding>() {

    private val viewModel: ClassRecordViewModel by viewModels()

    private val adapter = BasicAdapter<ClassRecord, FragmentClassListItemBinding> { holder, item ->
        holder.binding.vDash.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = if (holder.absoluteAdapterPosition == 0) 12 else 0
        }
        holder.binding.itemTitle.text = item.time.toMMddnyyyy
        holder.binding.itemTips.text = item.title
        holder.binding.itemTips.isVisible = true

        holder.binding.itemTips.setOnClickListener {
            startFragment(ClassRecordDetailFragment::class.java, bundleOf(EXTRA_DATA to item))
        }
        val childAdapter = BasicAdapter<List<String>, FragmentClassRecordItemBinding> { holder1, childItem ->
            holder1.binding.itemImage2.isInvisible = childItem.size <= 1
            holder1.binding.itemImage3.isInvisible = childItem.size <= 2
            holder1.binding.itemImage4.isInvisible = childItem.size <= 3
            holder1.binding.itemArrow.isInvisible = childItem.size <= 4
            holder1.binding.itemArrow.text = "+${childItem.size - 4}"

            fun onClick(index: Int) {
                val image = holder1.binding.root.getChildAt(index) as SimpleDraweeView
                childItem.getOrNull(index)?.let {
                    image.isVisible = true
                    image.setImageURI(it)
                    image.setOnClickListener {
                        PictureActivity.startActivity(requireActivity(), ArrayList(childItem.toList()), index)
                    }
                } ?: kotlin.run {
                    image.isInvisible = true
                }
            }
            onClick(0)
            onClick(1)
            onClick(2)
            onClick(3)
        }
        holder.binding.itemRecycler.let { recyclerView ->
            recyclerView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin =dp2px( 20.dp).toInt()
            }
            recyclerView.adapter = childAdapter
        }
        childAdapter.refresh(mutableListOf(item.picList?.map { it.url } ?: mutableListOf()))
        holder.itemView.setOnClickListener {
            startFragment(ClassRecordDetailFragment::class.java, bundleOf(EXTRA_DATA to item))
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val tabs = resources.getStringArray(R.array.class_tabs)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                getString(R.string.class_7_day) -> viewModel.load(type = 1, text = text, 1)
                getString(R.string.class_3_month) -> viewModel.load(type = 2, text = text, 1)
                getString(R.string.class_current_semester) -> viewModel.load(type = 3, text = text, 1)
                getString(R.string.class_all) -> viewModel.load(type = 0, text = text, 1)
            }
        }

        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, page = 1)
            false
        }

        viewModel.data.observe(viewLifecycleOwner) {
            if (it.first == 1) {
                adapter.refresh(it.second)
                if (it.second.isNotEmpty()) {
                    lifecycleScope.launch(CoroutineExceptionHandler()) {
                        binding.recycler.smoothScrollToPosition(0)
                    }
                }
            } else {
                adapter.addAll(it.second)
            }
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.load(page = 1)
        }
    }
}
